package com.ebook.server.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ebook.core.domain.Book;
import com.ebook.server.service.BookService;
import com.ebook.server.service.TestService;
import com.ebook.server.service.UserService;
import com.ebook.util.Status;
import com.ebook.util.ZipUtils;

@Controller
@RequestMapping(value = "/test")
public class TestController {
	
	@Resource
	private BookService bookService;
	
	@Resource
	private TestService testService;
	
	@Resource
	private UserService userService;
	

	public static final String UPLOAD_TEMP_PATH = "./";
	
	private static Log logger = LogFactory.getLog(TestController.class);
	
	@RequestMapping(value = "/deCompressZip")
	public ModelAndView deCompressZip(@RequestParam("bookid") int bookid,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelView = new ModelAndView();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String status = Status.SUCCESS;
		Book book = bookService.getBookInfo(bookid);
		String zipPath = "/opt/ebook/data/ebooks/"+bookid+"_"+book.getName()+".epub";
		StringBuilder deCompressPath = new StringBuilder("/opt/ebook/data/ebooks/before_unzip/");
		deCompressPath.append(bookid).append("_").append(book.getName());
//		StringBuilder deCompressPath = new StringBuilder(zipPath.substring(0, zipPath.lastIndexOf("/")+1));
//		deCompressPath.append("unzip/").append(zipPath.substring(zipPath.lastIndexOf("/")+1,zipPath.lastIndexOf(".")));
		logger.info("要解压的zip文件路径："+zipPath);
		logger.info("解压后的文件路径："+deCompressPath.toString());
		long start = System.currentTimeMillis();
		try {
			
			ZipUtils.deCompressZip(zipPath, deCompressPath.toString());
			
		} catch (IOException e) {
			logger.error(e);
			e.printStackTrace();
			status = "Fail";	
		} catch (ArchiveException e) {
			e.printStackTrace();
			status = "Fail";
			logger.error(e);
		}
		long end = System.currentTimeMillis();
		logger.info("解压用时："+(end-start)+" ms");
		modelMap.put("status", status);
		modelMap.put("Time_consuming", (end-start));
		modelMap.put("zipPath", zipPath);
		modelMap.put("deCompressPath", deCompressPath.toString());
		modelView.addAllObjects(modelMap);
		return modelView;
	}
	
	@RequestMapping(value = "/compressZip")
	public ModelAndView compressZip(@RequestParam("uid") int uid, @RequestParam("bookid") int bookid,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modelView = new ModelAndView();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		String status = Status.SUCCESS;
		String filePath = "/opt/ebook/data/ebooks/unzip/"+uid+"_"+bookid;
		String zipPath = "/opt/ebook/data/ebooks/afterzip/"+uid+"_"+bookid+".zip";
		logger.info("要压缩的文件路径："+filePath);
		logger.info("压缩后的zip文件路径："+zipPath);
		long start = System.currentTimeMillis();
			try {
				ZipUtils.compress(filePath, zipPath);
			} catch (Exception e){
				logger.error(e);
				status = "Fail";
			}
			long end = System.currentTimeMillis();
			logger.info("压缩用时："+(end-start)+" ms");
		modelMap.put("status", status);
		modelMap.put("Time_consuming", (end-start));
		modelMap.put("filePath", filePath);
		modelMap.put("zipPath", zipPath);
		modelView.addAllObjects(modelMap);
		return modelView;
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpServletRequest request){
		ModelAndView modelView = new ModelAndView();
		Map<String,Object> modelMap = new HashMap<String,Object>();
		modelMap.put("flag", testService.add());
		modelView.addAllObjects(modelMap);
		return modelView;
	}
	
	@RequestMapping(value = "/updateCode")
	public ModelAndView updateCode(HttpServletRequest request){
		ModelAndView modelView = new ModelAndView();
		Map<String,Object> modelMap = new HashMap<String,Object>();
		userService.setCode();
		modelView.addAllObjects(modelMap);
		modelMap.put("status", Status.SUCCESS);
		return modelView;
	}
	
	@RequestMapping(value = "/updateBookPath")
	public ModelAndView updateBookPath(HttpServletRequest request){
		ModelAndView modelView = new ModelAndView();
		Map<String,Object> modelMap = new HashMap<String,Object>();
//		userService.setCode();
		bookService.setBookPath();
		modelView.addAllObjects(modelMap);
		modelMap.put("status", Status.SUCCESS);
		return modelView;
	}
	
	@RequestMapping(value = "/getdata")
	public ModelAndView getdata(){
		ModelAndView modelView = new ModelAndView();
		userService.list();
		return modelView;
	}
	
}
